cmake_minimum_required(VERSION 3.16)
project(AGV_System VERSION 1.0.0 LANGUAGES CXX)

# 设置C++标准
set(CMAKE_CXX_STANDARD 20)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
set(CMAKE_CXX_EXTENSIONS OFF)

# 确保一致的调试设置
if(CMAKE_BUILD_TYPE STREQUAL "Debug")
    add_definitions(-D_DEBUG -DDEBUG)
    # 设置迭代器调试级别
    add_definitions(-D_ITERATOR_DEBUG_LEVEL=2)
else()
    add_definitions(-DNDEBUG)
    add_definitions(-D_ITERATOR_DEBUG_LEVEL=0)
endif()

# 启用 Qt 自动 MOC 处理
set(CMAKE_AUTOMOC ON)
set(CMAKE_AUTORCC ON)
set(CMAKE_AUTOUIC ON)

# 全局设置使用静态 CRT
if(MSVC)
    # 启用 UTF-8 支持
    add_compile_options(/utf-8)
    add_compile_options(/EHsc)
endif()

# 启用测试
# enable_testing()

# 查找Qt6组件
find_package(QT NAMES Qt6 REQUIRED COMPONENTS Core Quick QuickControls2 Network Sql)
find_package(Qt${QT_VERSION_MAJOR} REQUIRED COMPONENTS Core Quick QuickControls2 Network Sql)

# 设置输出目录
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin)
set(CMAKE_LIBRARY_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/lib)
set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/lib)
# 包含第三方依赖库
add_subdirectory(dep)

# Make Boost headers visible to all subdirectories if available
if(TARGET Boost::headers)
    get_target_property(_boost_inc Boost::headers INTERFACE_INCLUDE_DIRECTORIES)
    if(_boost_inc)
        include_directories(${_boost_inc})
        message(STATUS "Root: Using Boost includes from Boost::headers -> ${_boost_inc}")
    endif()
elseif(DEFINED Boost_INCLUDE_DIRS)
    include_directories(${Boost_INCLUDE_DIRS})
    message(STATUS "Root: Using Boost include dirs -> ${Boost_INCLUDE_DIRS}")
elseif(EXISTS "${DEP_PLATFORM_DIR}/boost/include/boost")
    include_directories("${DEP_PLATFORM_DIR}/boost/include")
    message(STATUS "Root: Fallback to vendored Boost includes -> ${DEP_PLATFORM_DIR}/boost/include")
endif()

# 构建src为一个库
add_subdirectory(src)

# 构建app引导程序
add_subdirectory(app)

# sandbox / tests helpers (optional)
if(EXISTS "${CMAKE_SOURCE_DIR}/tests/CMakeLists.txt")
    add_subdirectory(tests)
endif()

# 复制配置文件到构建输出目录
file(COPY ${CMAKE_SOURCE_DIR}/config
     DESTINATION ${CMAKE_RUNTIME_OUTPUT_DIRECTORY})

message(STATUS "配置文件将被复制到: ${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/config")
